package com.share.common.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * @author syq
 * @data 2019/7/27
 */
@Data
@ApiModel("分页请求")
public class PageReq implements Serializable {
    @ApiModelProperty("第几页")
    protected int pageNum = 1;

    @ApiModelProperty("一页大小")
    protected int pageSize = 20;

    @ApiModelProperty("排序字段eg: create_time desc")
    protected String orderBys;

    @ApiModelProperty("需要的列 eg: create_time")
    protected String columns;

    /**
     * 对orderBys字段进行转换, 返回的列中带空格的为正序
     * req.convertOrders().forEach(c -> abstractWrapper.orderBy(c != null, c.contains(" "), c));
     */
    public List<String> convertOrders() {
        List<String> reList = new LinkedList<>();
        if (orderBys == null || orderBys.trim().length() == 0) {
            return reList;
        }
        String[] arr = orderBys.split(",");
        for (String item : arr) {
            String tmpStr = item.trim();
            if (tmpStr.length() == 0) {
                continue;
            }
            String[] kv = tmpStr.split(" ");
            String col = null;
            if (kv.length == 1) {
                col = kv[0] + " ";
            } else if (kv.length > 1) {
                String orderStr = item.substring(item.indexOf(kv[0]) + kv[0].length());
                if (orderStr.toLowerCase().contains("des")) {
                    col = kv[0];
                } else if (orderStr.toLowerCase().contains("aes")) {
                    col = kv[0] + " ";
                } else {
                    //默认正序
                    col = kv[0] + " ";
                }
            }
            reList.add(col);
        }
        return reList;
    }
}
